package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-12-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    /**
     * 根据id查询课程大纲列表
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据id查询所有章节
        LambdaQueryWrapper<EduChapter> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getCourseId,courseId);
        List<EduChapter> listAll = this.list(queryWrapper);

        List<ChapterVo> finalList;
        finalList=listAll.stream().map((item)->{
            ChapterVo chapterVo=new ChapterVo();
            List<VideoVo> videoVoList=new ArrayList<>();
            BeanUtils.copyProperties(item,chapterVo);
            LambdaQueryWrapper<EduVideo> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(EduVideo::getChapterId,item.getId());
            List<EduVideo> videoList = videoService.list(queryWrapper1);
            for (EduVideo video: videoList
                 ) {
                VideoVo videoVo=new VideoVo();
                BeanUtils.copyProperties(video,videoVo);
                videoVoList.add(videoVo);
            }
            chapterVo.setChildren(videoVoList);
            return chapterVo;
        }).collect(Collectors.toList());
        return finalList;
    }

    /**
     * 删除章节
     * @param chapterId
     * @return
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        LambdaQueryWrapper<EduVideo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getChapterId,chapterId);
        int count = videoService.count(queryWrapper);
        //查出小节，不进行删除,查不出再删
        if(count>0){
            throw new GuliException(20001,"不能删除");
        }else {
            boolean result = this.removeById(chapterId);
            return result;
        }

    }

    /**
     * 根据课程id删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        LambdaQueryWrapper<EduChapter> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getCourseId,courseId);
        this.remove(queryWrapper);
    }
}
